﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PS3Helpers;
using PS3Helpers.Helpers;
using System.IO;
using FTPLib;

namespace PS3Packager
{
    public class PackageUploadThread : ThreadBase
    {
        private readonly OutputLogInterface _logInterface;
        private readonly string _sourceFilename;
        private readonly string _targetDirectory;
        private readonly Uri _targetUri;

        public PackageUploadThread(OutputLogInterface loginterface, string sourcefilename, string targetdirectory)
        {
            _logInterface = loginterface;
            _sourceFilename = sourcefilename;
            _targetDirectory = targetdirectory;
        }

        public PackageUploadThread(OutputLogInterface loginterface, string sourcefilename, Uri targeturi)
        {
            _logInterface = loginterface;
            _sourceFilename = sourcefilename;
            _targetUri = targeturi;
        }

        protected override void Execute()
        {
            if (!string.IsNullOrEmpty(_targetDirectory))
            {
                _logInterface.SetCurrentStep("Copy package to local folder");

                string target = _targetDirectory;
                target = target.TrimEnd(Path.PathSeparator) + Path.PathSeparator;
                target += Path.GetFileName(_sourceFilename);

                File.Copy(_sourceFilename, target, true);
            }

            if (_targetUri != null)
            {
                _logInterface.SetCurrentStep("Upload package to PS3");
                _logInterface.SetCurrentStatus("Connecting");

                FTP ftp = new FTP(_targetUri);
                ftp.PassiveMode = false;
                try
                {
                    ftp.Connect();

                    
                    string folder = _targetUri.AbsolutePath;
                    if (string.IsNullOrEmpty(folder))
                        folder = "/dev_hdd0/vsh/task"; // Should we allow uploading to the root?

                    folder = folder.Replace('\\', '/');
                    folder = folder.TrimEnd('/');
                    // folder = folder + "/" + _packageFile.ContentId;

                    
                    _logInterface.SetCurrentStatus("Changing directory");
                    try
                    {
                        ftp.ChangeDir(folder);
                    }
                    catch
                    {
                        // ftp.ChangeDir("/dev_hdd0/vsh/task");
                        ftp.MakeDir(folder);

                        ftp.ChangeDir(folder);
                    }

                    _logInterface.SetCurrentStatus("Uploading");

                    ftp.OpenUpload(_sourceFilename, false);
                    while (ftp.DoUpload() > 0)
                    {
                        if (Terminated)
                        {
                            // This will result in a partially uploaded file left on the ftp server (the PS3)
                            break;
                        }

                        base.Sleep(10);
                    }
                }
                finally
                {
                    ftp.Disconnect();
                }
            }
        }
    }
}
